import ConfigParser

conf = ConfigParser.ConfigParser()
conf.read('presets/winamp_presets')
def hatar(val):
    return max(-20.0, min(20.0, val))
sections = [section for section in conf.sections() if section != 'Presets']
for section in sections:
    f60 = conf.getfloat(section, '60')
    f170 = conf.getfloat(section, '170')
    df = f60-f170
    for freq in (20, 25, 31, 31.5, 40, 50):
        conf.set(section, str(freq), str((60.0/freq)*df/(170/60.0)+f60))
    fr_map = ( ((60,170), (62, 63, 80, 100, 125, 160)),
            ((170, 310), (200, 250)),
            ((310, 600), (315, 400,500)),
            ((600, 1000), (630, 800)),
            ((1000, 3000), (1250, 1600, 2000, 2500)),
            ((3000, 6000), (3150, 4000, 5000)),
            ((6000, 12000), (6300, 8000, 10000)),
            ((12000, 14000), (12500,)))
    str_map = {1000: '1k', 
            1250: '1.25k',
            1600: '1.6k',
            2000: '2k',
            2500: '2.5k',
            3000: '3k',
            3150: '3.15k',
            4000: '4k',
            5000: '5k',
            6000: '6k',
            6300: '6.3k',
            8000: '8k',
            10000: '10k',
            12000: '12k',
            12500: '12.5k',
            14000: '14k',
            16000: '16k',
            20000: '20k'}
    for freqs, tocount in fr_map:
        f1 = freqs[0]
        f2 = freqs[1]
        if f1 >= 1000:
            f1_name = str_map[f1]
        else:
            f1_name = str(f1)
        if f2 >= 1000:
            f2_name = str_map[f2]
        else:
            f2_name = str(f2)
        df = conf.getfloat(section, f2_name)-conf.getfloat(section, f1_name)

        for freq in tocount:
            if freq >= 1000:
                fr_name = str_map[freq]
            else:
                fr_name = str(freq)
            conf.set(section, fr_name, str(hatar((freq*1.0/f1)*df/(f2*1.0/f1)+conf.getfloat(section, f1_name))))
    f14 = conf.getfloat(section, '14k')
    f16 = conf.getfloat(section, '16k')
    df = f16-f14
    conf.set(section, '20k', str(hatar((20000.0/freq)*df/(16000/14000.0)+f16)))
import sys
with open('eq_presets.conf', 'w') as fd: 
    conf.write(fd)
